﻿Imports System.ComponentModel.DataAnnotations
Imports MainLib.Helpers
Imports System.Text.RegularExpressions


Public Class RequiredAtt
    Inherits RequiredAttribute

    Sub New(Optional keyField As String = "")
        Me.ErrorMessage = String.Format(translate("field_required"), translate(keyField))
    End Sub

End Class



Public Class StringLengthAtt
    Inherits StringLengthAttribute


    Sub New(MaximumLength As Integer, Optional keyField As String = "", Optional MinimumLength As Integer = 0)
        MyBase.New(MaximumLength)

        Try
            Me.MinimumLength = MinimumLength

            If MinimumLength = 0 Then
                Me.ErrorMessage = String.Format(translate("field_string_length_max"), translate(keyField), "{1}")
            Else
                Me.ErrorMessage = String.Format(translate("field_string_length_max_min"), translate(keyField), "{1}", "{2}")
            End If
        Catch ex As Exception
        End Try
    End Sub

End Class



Public Class RangeAtt
    Inherits RangeAttribute

    Sub New(minimum As Integer, maximum As Integer, Optional keyField As String = "")
        MyBase.New(minimum, maximum)
        Try
            Me.ErrorMessage = String.Format(translate("field_range"), translate(keyField), "{1}", "{2}")
        Catch ex As Exception
        End Try
    End Sub

End Class



<AttributeUsage(AttributeTargets.[Property] Or AttributeTargets.Field)> _
Public Class EmailAddressAtt
    Inherits ValidationAttribute


    Sub New(Optional keyField As String = "")
        Me.ErrorMessage = String.Format(translate("field_email"), translate(keyField))
    End Sub


    Public Overrides Function IsValid(ByVal value As Object) As Boolean
        Dim result As Boolean = True
        If value IsNot Nothing Then
            result = IsEmail(value.ToString)
        End If
        Return result
    End Function


    Public Overrides Function FormatErrorMessage(ByVal name As String) As String
        Return ErrorMessageString
    End Function


    Private Function IsEmail(ByVal email As String) As Boolean
        Static emailExpression As New Regex("^[_a-z0-9-]+(.[a-z0-9-]+)@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})$")
        Return emailExpression.IsMatch(email)
    End Function


End Class

Public Class PasswordAtt
    Inherits ValidationAttribute

    Sub New(MinimumLength As Integer, Optional keyField As String = "", Optional MaximumLength As Integer = 10)
        Me.ErrorMessage = String.Format(translate("field_password"), translate(keyField))
    End Sub


    Public Overrides Function IsValid(ByVal value As Object) As Boolean
        Dim result As Boolean = True
        If value IsNot Nothing Then
            result = IsPassword(value.ToString)
        End If
        Return result
    End Function


    Public Overrides Function FormatErrorMessage(ByVal name As String) As String
        Return ErrorMessageString
    End Function

    Private Function IsPassword(ByVal password As String) As Boolean
        Static passwordExpression As New Regex("(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}")
        Return passwordExpression.IsMatch(password)
    End Function
End Class